//
// Created by Cammy on 2024/4/24.
//
#include "BidList.h"

BidListNode *BuyBidListNode(BidListType x) {
    BidListNode *newnode = (BidListNode *) malloc(sizeof(BidListNode));
    newnode->data = x;
    newnode->prev = NULL;
    newnode->next = NULL;

    return newnode;
}

BidListNode *ListInit() {
    BidListNode *phead = BuyBidListNode(0);
    phead->prev = phead;
    phead->next = phead;
    return phead;
}

void ListPrint(BidListNode *phead) {
    BidListNode *cur = phead->next;
    if (cur == phead) {
        printf("NULL");
    } else {
        while (cur != phead) {
            printf("%d -> ", cur->data);
            cur = cur->next;
        }
    }

    printf("\n");

}


void ListDestroy(BidListNode *phead) {
    assert(phead);
    BidListNode *cur = phead->next;
    while (cur != phead) {
        BidListNode *next = cur->next;
        free(cur);
        cur = next;
    }

    free(phead);
    phead = NULL;
}

// 尾插
void ListPushBack(BidListNode *phead, BidListType x) {
    assert(phead);
//    2
    ListInsert(phead, x);


//    1
//    BidListNode *newnode= BuyBidListNode(x);
//    BidListNode *tail=phead->prev;
//
//    tail->next=newnode;
//    newnode->prev=tail;
//    newnode->next=phead;
//    phead->prev=newnode;
}

// 头插
void ListPushFront(BidListNode *phead, BidListType x) {
    assert(phead);
//    2
    ListInsert(phead->next, x);

//    1
//    BidListNode *newnode= BuyBidListNode(x);
//    BidListNode *first=phead->next;
//
//    first->prev=newnode;
//    newnode->next=first;
//    newnode->prev=phead;
//    phead->next=newnode;

//    newnode->next=phead->next;
//    phead->next->prev=newnode;
//    phead->next=newnode;
//    newnode->prev=phead;
}

// 尾删
void ListPopBack(BidListNode *phead) {
    assert(phead);
    assert(phead->prev != phead);
//    2
    ListErase(phead->prev);

//    1
//    BidListNode *deltail=phead->prev;
//    BidListNode *prev=deltail->prev;
//
//    prev->next=phead;
//    phead->prev=prev;
//
//    free(deltail);
//    deltail=NULL;

}

// 头删
void ListPopFront(BidListNode *phead) {
    assert(phead);
    assert(phead->next != phead);
//    3
    ListErase(phead->next);

//    2
//    BidListNode *del_first=phead->next;
//    BidListNode *next=del_first->next;
//
//    next->prev=phead;
//    phead->next=next;
//
//    free(del_first);
//    del_first=NULL;


//    1
//    if(phead->next!=phead){
//        BidListNode *del=phead->next;
//        BidListNode *flag=del->next;
//
//        phead->next=flag;
//        flag->prev=phead;
//
//        free(del);
//        del=NULL;
//    }else{
//        printf("BidList is Empty...");
//    }
}

BidListNode *ListFind(BidListNode *phead, BidListType x) {
    assert(phead);

    BidListNode *cur = phead->next;
    while (cur != phead) {
        if (cur->data == x) {
            return cur;
        }
        cur = cur->next;
    }

    return NULL;
}


// pos位置之前插入x
void ListInsert(BidListNode *pos, BidListType x) {
    assert(pos);

    BidListNode *newnode = BuyBidListNode(x);
    BidListNode *prev = pos->prev;

    prev->next = newnode;
    newnode->prev = prev;
    newnode->next = pos;
    pos->prev = newnode;

}

// 删除pos位置的值
void ListErase(BidListNode *pos) {
    assert(pos);

    BidListNode *prev = pos->prev;
    BidListNode *next = pos->next;

    prev->next = next;
    next->prev = prev;

    free(pos);
    pos = NULL;
}

// 判空
bool isEmpty(BidListNode *phead) {
    if (phead->next == phead) {
        return true;
    } else {
        return false;
    }
}

// 长度
int Size(BidListNode *phead) {
    assert(phead);
    int size = 0;
    BidListNode *cur = phead->next;
    while (cur != phead) {
        size++;
        cur = cur->next;
    }
    return size;
}